//
//  STCustomTextView.swift
//  Demo-04-TabBarTest
//
//  Created by 苏统世 on 16/5/21.
//  Copyright © 2016年 SuTon. All rights reserved.
//

import UIKit

private let placeholdTextFont:CGFloat = 15.0

class STCustomTextView: UITextView {

    /// 占位文本
    var placeholderText: String="请输入你的评论，记得好评喔"{
        didSet {
            // 别人设置文字.将文字设置给label
            placeholderLabel.text = placeholderText
        }
    }
    // MARK: - 懒加载
    /// 占位label, 不要直接暴露lable给别人,防止别人做其他的操作
    private lazy var placeholderLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(placeholdTextFont)
        label.text = self.placeholderText
        label.textColor = UIColor.lightGrayColor()
        return label
    }()
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
        /// 添加通知 当编辑时隐藏 占位文本Label
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("hiddenPlaceholdLabel"), name: UITextViewTextDidChangeNotification, object: self)
    }
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    @objc private func hiddenPlaceholdLabel(){
        placeholderLabel.hidden = hasText()
    }
    // MARK: - 设置UI
    private func setupUI(){
        backgroundColor=UIColor.cyanColor()
        addSubview(placeholderLabel)
        
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(6)
            make.leading.equalTo(self).offset(8)
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
